﻿using DotNetCore.CAP;
using Windows.Event.Share;
using Windows.Log.Domain;
using Windows.Log.Infrastructure.EFCore;

namespace Windows.Log.Service
{
    public class LogOperateAddEventHandler : IEventHandler<LogOperateAddEvent>
    {
        private readonly LogDbContext _db;

        public LogOperateAddEventHandler(LogDbContext db)
        {
            _db = db;
        }
        [CapSubscribe(nameof(LogOperateAddEvent))]
        public async Task Handler(LogOperateAddEvent eventData)
        {
            LogOperate model = new()
            {
                UserName = eventData.UserName,
                IP = eventData.IP,
                City = eventData.City,
                UserAgent = eventData.UserAgent,
                Browser = eventData.Browser,
                Os = eventData.Os,
                ElapsedMilliseconds = eventData.ElapsedMilliseconds,
                ApiName = eventData.ApiName,
                ApiUrl = eventData.ApiUrl,
                ApiMethod = eventData.ApiMethod,
                Request = eventData.Request,
                Response = eventData.Response,
                IsSuccess = eventData.IsSuccess,
                ErrorMessage = eventData.ErrorMessage,
                Exception = eventData.Exception,
            };
            await _db.AddAsync(model);
            await _db.SaveChangesAsync();
        }
    }
}
